package com.ctvit.sjk.knowledge.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.sjk.knowledge.app.mapper.TabAppChatMapper;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabApp;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppChat;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppChatMessageDTO;
import com.ctvit.sjk.knowledge.app.service.ITabAppChatRecordService;
import com.ctvit.sjk.knowledge.app.service.ITabAppChatService;
import com.ctvit.sjk.knowledge.app.service.ITabAppService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 应用聊天Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Slf4j
@Service
public class TabAppChatServiceImpl extends ServiceImpl<TabAppChatMapper, TabAppChat> implements ITabAppChatService {

    private final ITabAppService tabAppService;

    private final ITabAppChatRecordService tabAppChatRecordService;

    public TabAppChatServiceImpl(ITabAppService tabAppService, ITabAppChatRecordService tabAppChatRecordService) {
        this.tabAppService = tabAppService;
        this.tabAppChatRecordService = tabAppChatRecordService;
    }

    @Override
    public String open(String appId, String clientId, String userId) {
        String chatId = IdWorker.getIdStr();

        LambdaQueryWrapper<TabApp> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(TabApp::getAppDesc);
        wrapper.eq(TabApp::getAppId, appId);
        TabApp appEntity = tabAppService.getOne(wrapper);

        TabAppChat tabAppChat = new TabAppChat();
        tabAppChat.setChatId(chatId);
        tabAppChat.setAppId(appId);
        tabAppChat.setChatDesc(appEntity.getAppDesc());
        tabAppChat.setClientId(clientId);
        tabAppChat.setCreateId(userId);
        tabAppChat.setCreateTime(LocalDateTime.now());
        tabAppChat.setUpdateId(userId);
        tabAppChat.setUpdateTime(LocalDateTime.now());
        save(tabAppChat);

        return chatId;
    }

    @Override
    public String message(String chatId, AppChatMessageDTO dto, String userId) {
        return StringUtils.EMPTY;
    }

}
